﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_F5LocalTroubles : Form
    {
        private List<string> serialList;
        private List<string> selectedSerial;

        private DataSet reason_ds;
        private DataViewManager reason_dsView;

        private DataSet mistake_ds;
        private DataViewManager mistake_dsView;

        private bool noserial;
        public frm_Add_F5LocalTroubles()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            this.reason_ds = new DataSet("MistakeReason");
            this.mistake_ds = new DataSet("Mistake");

            this.noserial = false;
            serialList = new List<string>();
            selectedSerial = new List<string>();            
            this.addDate.EditValue = DateTime.Now.Date;
            getOrderNoList();
            this.orderNo.Properties.AutoComplete = true;
            getCheckerList();
            this.CheckerID.Properties.AutoComplete = true;
            getCustomerList();

            getReasonCode();
            getMistakeCode();
        }

        ////////////////////////////////////////////////
        /// Load Reason Code to form
        ////////////////////////////////////////////////
        private void getReasonCode()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT ReasonCode, ReasonName FROM [231_MistakeReason] ORDER BY ReasonCode";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "MistakeReason");
                da1.Fill(reason_ds);
                reason_dsView = reason_ds.DefaultViewManager;

                cmd_sql.Dispose();
                myConnection.Close();

                this.ReasonCode.DataSource = reason_dsView;
                this.ReasonCode.DisplayMember = "MistakeReason.ReasonCode";
                this.ReasonCode.ValueMember = "MistakeReason.ReasonCode";
                this.ReasonCode.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.troubleReason.DataBindings.Add("Text", reason_dsView, "MistakeReason.ReasonName");

            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot Reason Code list");
            }
        }

        ////////////////////////////////////////////////
        /// Load Mistake Code to form
        ////////////////////////////////////////////////
        private void getMistakeCode()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT MistakeCode, MistakeName FROM [231_Mistake] ORDER BY MistakeCode";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "Mistake");
                da1.Fill(mistake_ds);
                mistake_dsView = mistake_ds.DefaultViewManager;

                cmd_sql.Dispose();
                myConnection.Close();

                this.NGCode.DataSource = mistake_dsView;
                this.NGCode.DisplayMember = "Mistake.MistakeCode";
                this.NGCode.ValueMember = "Mistake.MistakeCode";
                this.NGCode.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.troubleContent.DataBindings.Add("Text", mistake_dsView, "Mistake.MistakeName");

            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Mistake list");
            }
        }
        
        ////////////////////////////////////////////////
        /// Load Order No list to form
        ////////////////////////////////////////////////
        private void getOrderNoList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT OrderNo, PartNo"
                    + " FROM [231_F5Plan] WHERE FinishedDate IS NOT NULL ORDER BY OrderNo";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {                    
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["OrderNo"].ToString();
                    item.Value = dataReader["OrderNo"].ToString();
                    this.orderNo.Properties.Items.Add(item);

                    ComboboxItem item2 = new ComboboxItem();
                    item2.Text = dataReader["PartNo"].ToString();
                    item2.Value = dataReader["PartNo"].ToString();
                    this.partNo.Properties.Items.Add(item2);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get OrderNo list");
            }
        }

        ////////////////////////////////////////////////
        /// Load Customer list to form
        ////////////////////////////////////////////////
        private void getCustomerList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CustomerID, CustomerName FROM [231_Customer]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CustomerName"].ToString();
                    item.Value = dataReader["CustomerID"].ToString();
                    this.customerID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Customer list");
            }
        }

        ////////////////////////////////////////////////
        /// Load Staff list to Checker combobox
        ////////////////////////////////////////////////
        private void getCheckerList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT StaffID, StaffName"
                    + " FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["StaffID"].ToString() + " | " + dataReader["StaffName"].ToString();
                    item.Value = dataReader["StaffID"].ToString();
                    this.CheckerID.Properties.Items.Add(item);
                    this.MadeByID.Properties.Items.Add(item);
                    this.ResponseID.Properties.Items.Add(item);
                    this.assPerson.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Checker list");
            }

            ComboboxItem kind = new ComboboxItem();
            kind.Text = "New";
            kind.Value = "New";
            this.kindList.Properties.Items.Add(kind);

            ComboboxItem kind2 = new ComboboxItem();
            kind2.Text = "Old";
            kind2.Value = "Old";
            this.kindList.Properties.Items.Add(kind2);
        } 

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            DateTime inputdate, changedate, reportdate, returndeadline, finisheddate, assdate;
            string order = null;
            string serial = null;
            int trouble_quantity = 0;
            int total_quantity = 0;
            string checker = null, madeby = null, response = null;
            string kind = null;
            string trouble = null;
            string ngcode = null, reasoncode = null, solution = null, ngtype = null;
            string reason = null;
            string remark = null;
            string checkno = null;
            string reportno = null;
            string assresult = null;
            string assperson = null;
            string assnote = null;
            string customerid = null;

            ComboboxItem item = new ComboboxItem();

            if (!DateTime.TryParse(this.addDate.DateTime.Date.ToString(), out inputdate))
            {
                error = "Please select Date";
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.orderNo.SelectedIndex < 0)
                {
                    error = "Please Select Order No.";
                }
                else
                {
                    item = (ComboboxItem)this.orderNo.SelectedItem;
                    order = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (!Int32.TryParse(this.quantityValue.Text, out total_quantity))
                {
                    error = "Invalid Total Quantity";
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (!Int32.TryParse(this.troubleQty.Text, out trouble_quantity))
                {
                    error = "Invalid NG Quantity";
                }                
                //trouble_quantity = selectedSerial.Count;               
            }

            if (string.IsNullOrEmpty(error))
            {
                if (trouble_quantity <= 0)
                {
                    error = "Troube quantity must be greater than 0";
                }
                else
                {
                    if (trouble_quantity > total_quantity)
                    {
                        error = "Trouble quantity cannot be greater than Total quantity";
                    }
                    else
                    {
                        for (int i = 0; i < selectedSerial.Count; i++)
                        {
                            if (i == 0)
                            {
                                serial += selectedSerial[i].ToString();
                            }
                            else
                            {
                                serial += "," + selectedSerial[i].ToString();
                            }
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.kindList.SelectedIndex < 0)
                {
                    error = "Please Select Kind.";
                }
                else
                {
                    item = (ComboboxItem)this.kindList.SelectedItem;
                    kind = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.CheckerID.SelectedIndex < 0)
                {
                    error = "Please Select Checker.";
                }
                else
                {
                    item = (ComboboxItem)this.CheckerID.SelectedItem;
                    checker = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {

                if (this.MadeByID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.MadeByID.SelectedItem;
                    madeby = item.Value.ToString();
                }
                
                if (this.ResponseID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.ResponseID.SelectedItem;
                    response = item.Value.ToString();
                }

                trouble = this.troubleContent.Text;
                reason = this.troubleReason.Text;
                remark = this.noteText.Text;
                ngcode = this.NGCode.Text;
                reasoncode = this.ReasonCode.Text;
                solution = this.SolutionText.Text;
                ngtype = this.NGType.Text;
                checkno = this.checkNo.Text;
                reportno = this.reportNo.Text;
                assresult = this.assResult.Text;
                if (this.assPerson.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.assPerson.SelectedItem;
                    assperson = item.Value.ToString();
                }
                assnote = this.assNote.Text;


                if (this.customerID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.customerID.SelectedItem;
                    customerid = item.Value.ToString();
                }

                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [rrc_database].[dbo].[231_F5Troubles] ([Date], [TimePlanNo], [SerialNo],"
                        + " [Kind], [ExportQuantity], [NGQuantity], [ResponsePerson], [NGCode], [ChangeDate], [MadeBy],"
                        + " [TroubleKind], [ReasonCode], [CustomerID], [Checker], [F4TroubleContent], [Reason], [Remark],"
                        + " [Solution], [ReturnDeadline], [ReportDate], [FinishedDate], [Type], [CheckNo], [ReportNo],"
                        + " [AssDate], [AssResult], [AssPerson], [AssNote])"
                        + " VALUES (@dateinput, '" + order + "', '" + serial + "', '" + kind + "', " + total_quantity + ","
                        + " " + trouble_quantity + ", '" + response + "', '" + ngcode + "', @changedate, '" + madeby + "',"
                        + " '" + ngtype + "', '" + reasoncode + "', '" + customerid + "', '" + checker + "', N'" + trouble + "',"
                        + " N'" + reason + "', N'" + remark + "', N'" + solution + "', @returndeadline, @reportdate,"
                        + " @finisheddate, 0, '" + checkno + "', '" + reportno + "', @assdate, N'" + assresult + "',"
                        + " '" + assperson + "', N'" + assnote + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@dateinput", SqlDbType.DateTime).Value = inputdate.Date;

                    if (this.ChangeDate.EditValue != null)
                    {
                        if (DateTime.TryParse(this.ChangeDate.DateTime.Date.ToString(), out changedate))
                        {
                            cmd_sql.Parameters.Add("@changedate", SqlDbType.DateTime).Value = changedate.Date;
                        }
                        else
                        {
                            cmd_sql.Parameters.Add("@changedate", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@changedate", SqlDbType.DateTime).Value = DBNull.Value;
                    }

                    if (this.ReturnDeadline.EditValue != null)
                    {
                        if (DateTime.TryParse(this.ReturnDeadline.DateTime.Date.ToString(), out returndeadline))
                        {
                            cmd_sql.Parameters.Add("@returndeadline", SqlDbType.DateTime).Value = returndeadline.Date;
                        }
                        else
                        {
                            cmd_sql.Parameters.Add("@returndeadline", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@returndeadline", SqlDbType.DateTime).Value = DBNull.Value;
                    }

                    if (this.ReportDate.EditValue != null)
                    {
                        if (DateTime.TryParse(this.ReportDate.DateTime.Date.ToString(), out reportdate))
                        {
                            cmd_sql.Parameters.Add("@reportdate", SqlDbType.DateTime).Value = reportdate.Date;
                        }
                        else
                        {
                            cmd_sql.Parameters.Add("@reportdate", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@reportdate", SqlDbType.DateTime).Value = DBNull.Value;
                    }

                    if (this.FinishedDate.EditValue != null)
                    {
                        if (DateTime.TryParse(this.FinishedDate.DateTime.Date.ToString(), out finisheddate))
                        {
                            cmd_sql.Parameters.Add("@finisheddate", SqlDbType.DateTime).Value = finisheddate.Date;
                        }
                        else
                        {
                            cmd_sql.Parameters.Add("@finisheddate", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@finisheddate", SqlDbType.DateTime).Value = DBNull.Value;
                    }

                    if (this.assDate.EditValue != null)
                    {
                        if (DateTime.TryParse(this.assDate.DateTime.Date.ToString(), out assdate))
                        {
                            cmd_sql.Parameters.Add("@assdate", SqlDbType.DateTime).Value = assdate.Date;
                        }
                        else
                        {
                            cmd_sql.Parameters.Add("@assdate", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                    }
                    else
                    {
                        cmd_sql.Parameters.Add("@assdate", SqlDbType.DateTime).Value = DBNull.Value;
                    }

                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("F5 Local Trouble added success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add F5 Local Trouble. Please try again.");
                }                 
            }
            else
            {
                MessageBox.Show(error);
            }
        }        

        /////////////////////////////////////
        /// When Change Order No
        /////////////////////////////////////
        private void orderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.orderNo.SelectedIndex > -1)
            {
                this.partNo.SelectedIndex = this.orderNo.SelectedIndex;
                createSerialList();
                //ComboboxItem part = (ComboboxItem)this.partNo.SelectedItem;
                //getMakerList(part.Value.ToString());
                if (!noserial)
                {
                    this.quantityValue.Text = this.serialList.Count.ToString();
                    this.troubleQty.Text = this.selectedSerial.Count.ToString();
                }
            }
        }

        /////////////////////////////////////////////////
        /// Create Serial No List with selected Order No
        /////////////////////////////////////////////////
        private void createSerialList()
        {
            serialList.Clear();
            serialNo.DataSource = null;
            selectedSerial.Clear();
            selectedSerialNo.DataSource = null;
            noserial = false;
            this.serialNo.Enabled = true;
            this.selectedSerialNo.Enabled = true;
            this.buttonSelectAll.Enabled = true;
            this.buttonSelect.Enabled = true;
            this.buttonDeselect.Enabled = true;
            this.buttonDeselectAll.Enabled = true;
            this.troubleQty.Enabled = false;
            string begin_serial = null;
            string end_serial = null;
            string prefix = null;
            string suffix = null;
            ComboboxItem order = (ComboboxItem)this.orderNo.SelectedItem;
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT FromSerialNo, ToSerialNo FROM [231_F5Plan] WHERE OrderNo='" + order.Value + "'";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                if (dataReader.Read())
                {
                    begin_serial = dataReader["FromSerialNo"].ToString();
                    end_serial = dataReader["ToSerialNo"].ToString();
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
            }

            if (string.IsNullOrEmpty(begin_serial) || string.IsNullOrEmpty(end_serial))
            {
                //error = "Cannot find serial number for selected Order No";
                this.noserial = true;
            }
            else
            {
                if ((begin_serial.Length != 7) || (end_serial.Length != 7))
                {
                    //error = "Invalid serial Format. Please check Serial For selected Order";
                    this.noserial = true;
                }
            }

            /// Create Serial List
            if (!noserial)
            {
                bool isHex = false;
                int begin_number = 0;
                int end_number = 0;
                string new_item = null;
                if (begin_serial.Substring(6, 1) == "V")
                {
                    isHex = false;
                    prefix = begin_serial.Substring(0, 2);
                    suffix = begin_serial.Substring(6, 1);
                    begin_number = Int32.Parse(begin_serial.Substring(2, 4));
                    end_number = Int32.Parse(end_serial.Substring(2, 4));
                }
                else
                {
                    prefix = begin_serial.Substring(0, 3);
                    suffix = null;
                    if (Int32.TryParse(begin_serial.Substring(3, 4), out begin_number) && Int32.TryParse(end_serial.Substring(3, 4), out end_number))
                    {
                        isHex = false;
                    }
                    else
                    {
                        isHex = true;
                        begin_number = Int32.Parse(begin_serial.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);
                        end_number = Int32.Parse(end_serial.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);
                    }
                }

                for (int i = begin_number; i <= end_number; i++)
                {
                    if (!isHex)
                    {
                        new_item = prefix + i.ToString("D4") + suffix;
                    }
                    else
                    {
                        new_item = prefix + i.ToString("X4") + suffix;
                    }
                    serialList.Add(new_item);
                }
                serialNo.DataSource = serialList;
                quantityValue.Text = serialList.Count.ToString();
                troubleQty.Text = "0";
            }
            else
            {
                this.serialNo.Enabled = false;
                this.selectedSerialNo.Enabled = false;
                this.buttonSelectAll.Enabled = false;
                this.buttonSelect.Enabled = false;
                this.buttonDeselect.Enabled = false;
                this.buttonDeselectAll.Enabled = false;
                getQuantity();
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        /// Get finished quantity when there is no Serial No for selected product
        ////////////////////////////////////////////////////////////////////////////
        private void getQuantity()
        {
            this.troubleQty.Enabled = true;
            string error = null;
            ComboboxItem order = (ComboboxItem)this.orderNo.SelectedItem;
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT FinishedQuantity FROM [231_F5Plan] WHERE OrderNo='" + order.Value + "'";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                if (dataReader.Read())
                {
                    this.quantityValue.Text = dataReader["FinishedQuantity"].ToString();
                    troubleQty.Text = "0";
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                error = "Cannot get finished quantity";
            }

            if (!string.IsNullOrEmpty(error))
            {
                MessageBox.Show(error);
            }
        }

        ////////////////////////////
        /// Select All Serial
        ////////////////////////////
        private void buttonSelectAll_Click(object sender, EventArgs e)
        {            
            foreach (string value in serialList)
            {
                selectedSerial.Add(value);
            }
            //selectedSerial = serialList;
            selectedSerialNo.DataSource = null;
            selectedSerial.Sort();
            selectedSerialNo.DataSource = selectedSerial;

            serialList.Clear();
            serialNo.DataSource = null;

            //this.quantityValue.Text = this.serialList.Count.ToString();
            this.troubleQty.Text = this.selectedSerial.Count.ToString();
        }

        /////////////////////////////////////
        /// Deselect all serial
        ////////////////////////////////////
        private void buttonDeselectAll_Click(object sender, EventArgs e)
        {
            foreach (string value in selectedSerial)
            {
                serialList.Add(value);
            }
            //selectedSerial = serialList;
            serialNo.DataSource = null;
            serialList.Sort();
            serialNo.DataSource = serialList;

            selectedSerial.Clear();
            selectedSerialNo.DataSource = null;

            //this.quantityValue.Text = this.serialList.Count.ToString();
            this.troubleQty.Text = this.selectedSerial.Count.ToString();
        }
        
        /////////////////////////////
        /// Select some serial
        /////////////////////////////
        
        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (serialNo.SelectedItems.Count > 0)
            {
                for (int i = 0; i < serialNo.SelectedItems.Count; i++)
                {
                    selectedSerial.Add(serialNo.SelectedItems[i].ToString());                    
                }
                for (int i = serialNo.SelectedItems.Count - 1; i >= 0 ; i--)
                {                    
                    serialList.Remove(serialNo.SelectedItems[i].ToString());
                }
                serialNo.DataSource = null;
                serialList.Sort();
                serialNo.DataSource = serialList;
                selectedSerialNo.DataSource = null;
                selectedSerial.Sort();
                selectedSerialNo.DataSource = selectedSerial;

                //this.quantityValue.Text = this.serialList.Count.ToString();
                this.troubleQty.Text = this.selectedSerial.Count.ToString();
            }            
        }

        /////////////////////////////
        /// Deselect some serial
        /////////////////////////////
        private void buttonDeselect_Click(object sender, EventArgs e)
        {
            if (selectedSerialNo.SelectedItems.Count > 0)
            {
                for (int i = 0; i < selectedSerialNo.SelectedItems.Count; i++)
                {
                    serialList.Add(selectedSerialNo.SelectedItems[i].ToString());
                }
                for (int i = selectedSerialNo.SelectedItems.Count - 1; i >= 0; i--)
                {
                    selectedSerial.Remove(selectedSerialNo.SelectedItems[i].ToString());
                }
                serialNo.DataSource = null;
                serialList.Sort();
                serialNo.DataSource = serialList;
                selectedSerialNo.DataSource = null;
                selectedSerial.Sort();
                selectedSerialNo.DataSource = selectedSerial;

                //this.quantityValue.Text = this.serialList.Count.ToString();
                this.troubleQty.Text = this.selectedSerial.Count.ToString();
            }
        }     
    }
}
